<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class PriceController extends Controller
{
    //

    //价格首页
    public function getIndex()
    {   
        //获取导航信息
        $navigation = $this -> navigation();

        //获取产地和市场价格信息
        $prices = DB::table('mprice')
            -> orderBy('time','desc')
            -> skip(0) -> take(10) -> get();
        //涨
        $up = DB::table('mprice')
            -> orderBy('year','desc')
            -> skip(0) -> take(10) -> get();
        //跌
        $down = DB::table('mprice')
            -> orderBy('year','asc')
            -> skip(0) -> take(10) -> get();

        //获取电商价格信息
        $stores = DB::table('medicinal')
            -> orderBy('ctime','desc')
            -> skip(0) -> take(10) -> get();
        // dd($stores);
        //按照销量排序获取电商信息
        $sales = DB::table('medicinal')
            -> orderBy('sales','desc')
            -> skip(0) -> take(10) -> get();

        //获取热点资讯
        $news = $this -> hotnews();

    	return view('Home.price.index',['prices' => $prices,'up' => $up,'down' => $down,'news' => $news,'stores' => $stores,'sales' => $sales,'navigation' => $navigation]);
    }

    //产地,市场价格
    public function getMore($from=1,$market=2)
    {
        //获取导航信息
        $navigation = $this -> navigation();

        //========获取信息
        $price = DB::table('mprice') -> orderBy('time','desc') -> paginate(25);

        //=======获取涨跌幅信息
        //涨
        $up = DB::table('mprice')
            -> orderBy('year','desc')
            -> skip(0) -> take(6) -> get();
        //跌
        $down = DB::table('mprice')
            -> orderBy('year','asc')
            -> skip(0) -> take(6) -> get();

        //地区数组
        $area = array('北京市','天津市','河北省','山西省','内蒙古自治区','辽宁省','吉林省','黑龙江省','上海市','江苏省','浙江省','安徽省','福建省','江西省','山东省','河南省','湖北省','湖南省','广东省','广西壮族自治区','海南省','重庆市','四川省','贵州省','云南省','西藏自治区','陕西省','甘肃省','青海省');

        //========获取品种信息
         $first = DB::table('varieties') -> select(DB::raw('first')) -> groupBy('first') -> get();
        // dd($first);
        //将first转换为数组
        foreach($first as $k => $v){
            $dd[] = $v -> first;
        }
        // dd($dd);
        //将所有的品名压入到数组中
        foreach($dd as $k => $v){
            $name[] = DB::table('varieties') -> select(DB::raw('name')) -> where('first',$v) -> get();
        }
        // dd($name);
        $size = '';
        //改为关联数组,下标是首字母
        foreach($dd as $k=>$v){
            for($t=0;$t<count($name[$k]);$t++){
                $size++;
                $data[$v][$t]=$name[$k][$t]->name;
            }
        }

        //获取热点资讯信息
        $news = $this -> hotnews();

        // 计数器
        $count = 1;
        //解析模板
        if($from == 3){
    	   return view('Home.price.from',['navigation' => $navigation,'first' => $first , 'name' => $data , 'prices' => $price,'up' => $up,'down' => $down,'news' => $news,'area' => $area,'count' => $count]);
        }else{
           return view('Home.price.market',['navigation' => $navigation,'first' => $first , 'name' => $data , 'prices' => $price,'up' => $up,'down' => $down,'news' => $news,'count' => $count]);
        }
    }

    //市场价格
    public function postAjax(Request $request)
    {
    	$name = $request -> name;
        $sc = substr($request -> sc,0,6);
        $area = substr($request -> area,0,6);
        // dd($area);
        $names = DB::table('mprice') -> where('mName',$name) -> orderBy('time','desc') -> get();
        // dd($names);
        $market = DB::table('mprice') -> where('market',$sc) -> orderBy('time','desc') -> get();

        $from = DB::table('mprice') -> where('from','like',$area.'%') -> orderBy('time','desc') -> get();

        $info[] = $names;
        $info[] = $market; 
        $info[] = $from;
        // dd($info);
        echo json_encode($info);

    }

    //电商价格
    public function getStore(Request $request)
    {   
        //获取导航信息
        $navigation = $this -> navigation();

        //========获取品种信息
        $first = DB::table('varieties') -> select(DB::raw('first')) -> groupBy('first') -> get();
        //获取价格信息
        $stores = DB::table('medicinal') ->orderBy('ctime','desc') -> paginate(25);
        //获取成交量信息
        $sales = DB::table('medicinal') -> orderBy('sales','desc') -> skip(0) -> take(8) -> get();
        // dd($stores);
        //获取热门资讯
        $news = $this -> hotnews();
        // dd($news);

    	return view('Home.price.store',['navigation' => $navigation,'first' => $first,'stores' => $stores,'sales' => $sales,'news' => $news]);

    }

    //热门资讯
    public function hotnews()
    {
        $news = DB::table('portalpost') -> select('id','title') -> where('hot','1') -> orderBy('ctime','desc') -> skip(0) -> take(8) -> get();
        // dd($news);
        return $news;
    }

    // 获取导航信息
    public function navigation()
    {
        //父类
        $ptype=DB::table('portaltype')->where('pid','0') -> get();
        // dd($ptype);
        // $lasttype = DB::table('portaltype') -> where('pid','0') -> skip(7) -> take(7) ->get();
        //子类
        $stype=DB::table('portaltype')->where('pid','!=','0')->get();

        $navigation[] = $ptype;
        $navigation[] = $stype;

        return $navigation;
    }
}
